<template>
  <div>
    <el-form :inline="true" style="margin-bottom: -15px" label-width="130px">
      <el-form-item style="margin-bottom: 5px">
        <el-input v-model="form.groupName" placeholder="组别姓名..." size="mini" clearable />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" style="margin-bottom: 5px" @click="handleClick">查询</el-button>
      </el-form-item>
      <div style="float:right;">
        <groupPop ref="pop" />
      </div>
    </el-form>
    <el-table
      size="mini"
      :data="tableData"
      :height="tableHeight"
      style="width: 100%"
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        prop="groupName"
        label="组别名称"
        width="200px"
      />
      <el-table-column
        prop="leaderName"
        width="200px"
        label="组长"
      />
      <el-table-column
        width="150px"
        label="状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.remove===0" type="success">
            正常
          </el-tag>
          <el-tag v-else type="danger">
            禁用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="members"
        width="500px"
        label="组内成员"
      />
      <el-table-column
        label="操作"
        width="150"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleChangeStatus(scope.row)">禁用/启动</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        style="margin-top: 10px"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next"
        background
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { findGroupByPage, active, remove } from '@/api/group'
import groupPop from './groupPop'
export default {
  components: {
    groupPop
  },
  data() {
    return {
      height: `${document.documentElement.clientHeight}`,
      tableHeight: 442,
      form: {
        groupName: ''
      },
      page: {
        currentPage: 1,
        total: 0,
        pageSize: 10
      },
      tableData: [],
      row: {}
    }
  },
  created() {
    this.height > 900 ? this.tableHeight = 750 : 442
  },
  mounted() {
    this.handleClick()
  },
  methods: {
    handleClick() {
      findGroupByPage(this.form, this.page.currentPage, this.page.pageSize).then(response => {
        this.tableData = response.content
        this.page.total = response.totalNum
      })
    },
    handleSizeChange(value) {
      this.page.pageSize = value
      this.handleClick()
    },
    handleCurrentChange(value) {
      this.page.currentPage = value
      this.handleClick()
    },
    handleEdit(row) {
      this.$refs.pop.clickNew(row)
    },
    handleChangeStatus(row) {
      const message = row.remove === 0 ? '此操作将禁用组别 ' + row.groupName + '，是否继续?' : '此操作将启用组别 ' + row.groupName + '，是否继续?'
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.remove === 0
          ? remove(row.groupId).then(this.showMessage).catch(this.showCancelMessage)
          : active(row.groupId).then(this.showMessage).catch(this.showCancelMessage)
      })
    },
    showMessage() {
      this.$message({
        type: 'success',
        message: '操作成功!'
      })
      this.handleClick()
    },
    showCancelMessage() {
      this.$message({
        type: 'info',
        message: '已取消删除'
      })
    }
  }
}
</script>
